﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.Temp
{
	public class ManagePipeSpecification
	{
		public ManagePipeSpecification(Autodesk.Revit.DB.Document document)
		{
			this.Doc = document;
		}

		private List<Element> GetAllPipeTypes()
		{
			return YJKElementFilter.GetElementByClass(typeof(PipeType), this.Doc);
		}

		public void DeleteMepSize(double nominalDiameter)
		{
			Transaction transaction = null;
			try
			{
				List<Element> allPipeTypes = this.GetAllPipeTypes();
				transaction = new Transaction(this.Doc, "删除管道规格");
				transaction.Start();
				foreach (Element element in allPipeTypes)
				{
                    UnifiedModified.DeleteMepSize(element as Autodesk.Revit.DB.Plumbing.PipeType, nominalDiameter);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				throw;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
		}

		public void AddMepSize(HYPipeSpecifications pipeSpecifications)
		{
			Transaction transaction = null;
			try
			{
				List<Element> allPipeTypes = this.GetAllPipeTypes();
				transaction = new Transaction(this.Doc, "添加管道规格");
				transaction.Start();
				foreach (Element element in allPipeTypes)
				{
                    Autodesk.Revit.DB.Plumbing.PipeType pipeType = element as Autodesk.Revit.DB.Plumbing.PipeType;
					HYPipeSpecifications addSpecification = this.GetAddSpecification(pipeType, pipeSpecifications);
					if (addSpecification.Count >= 1)
					{
						foreach (HYPipeSpecification hypipeSpecification in addSpecification)
						{
							UnifiedModified.AddMepSize(pipeType, (double)hypipeSpecification.DN / 304.8, hypipeSpecification.Diameter / 304.8, hypipeSpecification.OutDiameter / 304.8);
						}
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				throw;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
		}

        private HYPipeSpecifications GetAddSpecification(Autodesk.Revit.DB.Plumbing.PipeType pipeType, HYPipeSpecifications pipeSpecification)
		{
			HYPipeSpecifications hypipeSpecifications = new HYPipeSpecifications();
			ICollection<MEPSize> allMepSize = UnifiedModified.GetAllMepSize(pipeType);
			foreach (HYPipeSpecification hypipeSpecification in pipeSpecification)
			{
				if (!this.BeExist(allMepSize, (double)hypipeSpecification.DN / 304.8))
				{
					hypipeSpecifications.Add(hypipeSpecification);
				}
			}
			return hypipeSpecifications;
		}

		private bool BeExist(ICollection<MEPSize> icoSize, double diameter)
		{
			bool result = false;
			IEnumerator<MEPSize> enumerator = icoSize.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (Math.Abs(enumerator.Current.NominalDiameter - diameter) < 1E-09)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public Document Doc { get; set; }

		public string MsgInfo { get; set; }
	}
}
